<?php

namespace App\Dao;

use App\Model\UserGag;
use App\Model\UserLock;
use Hyperf\Context\ApplicationContext;

class UserGagDao
{
    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }

    // 被禁言的uid 数组
    public function checkuid($type)
    {
        $result = UserGag::query()->where('type', $type)->where('status', 1)->get()->toArray();
        $data = array();
        if ($result) {
            foreach ($result as $v) {
                $data[] = $v['uid'];
            }
        }
        return $data;
    }

    // 被禁言imei 数组
    public function checkimei($type)
    {
        $result = UserGag::query()->where('type', $type)->where('status', 1)->get()->toArray();
        $data = array();
        if (!empty($result)) {
            foreach ($result as $v) {
                $data[] = $v['imei'];
            }
        }

        return $data;
    }

    public function gag($type)
    {
        // $result = Mem::get("user_gag_" . $type);
        $result = false;
        if ($result === false) {
            $result = array();
            // type=1表示禁言uid
            if ($type == 1) {
                $result = $this->checkuid($type);
            } elseif ($type == 2) {
                // type=2表示禁言uid
                $result = $this->checkimei($type);
            }
            // Mem::set("user_gag_" . $type, $result, 300);
        }
        return $result;
    }

    /**
     * 判断用户是否禁言
     * @param $uid
     * @param $type
     * @return bool
     */
    public function checkUidGag($uid, $type): bool
    {
        $exists = UserGag::query()->where('type', $type)->where('uid', $uid)->where('status', 1)->exists();
        if ($exists) {
            return true;
        } else {
            $lock = UserLock::query()->where('type', 2)->where('uid', $uid)->where('status', 1)->exists();
            if ($lock) {
                return true;
            } else {
                return false;
            }
        }
    }


    /**
     * 判断用户是否在禁言列表中
     * @param $imei
     * @param $type
     * @return bool
     */
    public function checkGag($val, $type)
    {
        if (in_array($type, [1, 2])) {
            $userGag = $this->gag($type);
            if (in_array($val, $userGag)) {
                return true;
            }
        }
        return false;

    }

}